package cn.bdqn.controller;

import cn.bdqn.dto.Dto;
import cn.bdqn.pojo.ItripHotelOrder;
import cn.bdqn.pojo.ItripHotelRoom;
import cn.bdqn.service.ItripHotelRoomService;
import cn.bdqn.service.OrderSearchService;
import cn.bdqn.util.Page;
import cn.bdqn.util.SystemConfig;
import cn.bdqn.vo.ItripHotelOrderEx;
import cn.bdqn.vo.ItripHotelOrderSearch;
import cn.bdqn.vo.ItripPersonalHotelOrderVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/8/20.
 */
@Api(value = "API", basePath = "")
@Controller
public class OrderSearchController {
    @Resource
    private OrderSearchService orderSearchService;
    @Resource
    private ItripHotelRoomService itripHotelRoomService;
    @Resource
    private SystemConfig systemConfig;
    /**
     * 订单查询
     *
     * @param itripHotelOrderSearch
     * @return
     */
    @ApiOperation(value = "订单查询", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "订单查询 ")
    @RequestMapping(value = "/api/hotelorder/getpersonalorderlist", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto getpersonalorderlist(
            @ApiParam(name = "itripHotelOrderEx", value = "封装了查询条件的实体类", required = true)
            @RequestBody ItripHotelOrderSearch itripHotelOrderSearch, HttpServletRequest request) {
        Dto dto = new Dto();
        Page page = null;
        String token=request.getHeader("token");
        String userId=token.split("-")[2];


            try {
                ItripHotelOrderEx itripHotelOrderEx = new ItripHotelOrderEx();
                //订单日期
                itripHotelOrderEx.setStartDate(itripHotelOrderSearch.getStartDate());
                //入住日期
                itripHotelOrderEx.setEndDate(itripHotelOrderSearch.getEndDate());
                //订单编号
                itripHotelOrderEx.setOrderNo(itripHotelOrderSearch.getOrderNo());
                //旅客姓名
                itripHotelOrderEx.setLinkUserName(itripHotelOrderSearch.getLinkUserName());


                itripHotelOrderEx.setOrderType(itripHotelOrderSearch.getOrderType());
                itripHotelOrderEx.setOrderStatus(itripHotelOrderSearch.getOrderStatus());

               itripHotelOrderEx.setUserId(Long.valueOf(userId));



                page = orderSearchService.getHotelOrderListByPage(itripHotelOrderSearch.getPageNo(),itripHotelOrderSearch.getPageSize(), itripHotelOrderEx);
                if(page.getRows()!=null){
                    dto.setCode("0000");
                    dto.setMsg("查询成功");
                    dto.setData(page);
                    return dto;
                }
                dto.setCode("10000");
                dto.setMsg("未找到相关订单信息");
                return dto;
            } catch (Exception e) {
                e.printStackTrace();
                dto.setCode("10086");
                dto.setMsg("查询异常");
                return dto;
            }

    }

    /**
     * 订单详情查看
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "订单详情", httpMethod = "get",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "订单详情 ")
    @RequestMapping(value = "/api/hotelorder/getpersonalorderinfo/{id}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto selectPersonalOrderInfo(
            @ApiParam(name = "id", value = "订单主键", required = true)
            @PathVariable long id) {
        Dto dto=new Dto();
        ItripPersonalHotelOrderVO orderVO=new ItripPersonalHotelOrderVO();
        try {
            ItripHotelOrder itripHotelOrder= orderSearchService.getByPrimaryKey(id);
            if(itripHotelOrder==null||itripHotelOrder.equals("")){
                dto.setCode("10000");
                dto.setMsg("未找到相关订单信息");
                return dto;
            }
            else {
                orderVO.setId(itripHotelOrder.getId());
                System.out.println(itripHotelOrder.getRoomId());
                ItripHotelRoom itripHotelRoom=itripHotelRoomService.senectById(itripHotelOrder.getRoomId());
                if (itripHotelRoom!=null&&!itripHotelRoom.equals("")){
                    orderVO.setRoomPayType(itripHotelRoom.getPayType());
                    System.out.println(orderVO.getPayType());
                }
                orderVO.setOrderNo(itripHotelOrder.getOrderNo());
                orderVO.setPayAmount(itripHotelOrder.getPayAmount());
                orderVO.setPayType(itripHotelOrder.getPayType());
                orderVO.setCreationDate(itripHotelOrder.getCreationDate());
                orderVO.setBookType(itripHotelOrder.getBookType());
                orderVO.setOrderStatus(itripHotelOrder.getOrderStatus());
                orderVO.setNoticePhone(itripHotelOrder.getNoticePhone());
                orderVO.setOrderStatus(itripHotelOrder.getOrderStatus());
                switch (orderVO.getOrderStatus()){
                    case 0:
                        orderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessCancel()));
                        orderVO.setProcessNode("2");
                        break;
                    case 1:
                        orderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessOK()));
                        orderVO.setProcessNode("3");
                        break;
                    case 2:
                        orderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessOK()));
                        orderVO.setProcessNode("3");
                        break;
                    case 3:
                        orderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessOK()));
                        orderVO.setProcessNode("5");
                        break;
                    case 4:
                        orderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessOK()));
                        orderVO.setProcessNode("6");
                        break;
                    default:
                        orderVO.setOrderProcess(null);
                        orderVO.setProcessNode(null);
                }
                dto.setCode("0000");
                dto.setMsg("已找到相关订单详情");
                dto.setData(orderVO);
                return dto;
            }
        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode("10086");
            dto.setMsg("系统异常");
            return dto;
        }

    }



}
